close all;
clearvars;

%% Datasets
% figure1: 3D PCK analysis of tracking with object interaction and wearing CHD
error_thresholds = [20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50];
free_motion_3DPCK = [0.7812, 0.8497, 0.8979, 0.9338, 0.9549, 0.9694, 0.9779, 0.9840, 0.9888, 0.9924, 0.9950, 0.9963, 0.9974, 0.9979, 0.9983, 0.9986];
object_interaction_3DPCK = [0.6928, 0.7823, 0.8488, 0.8932, 0.9209, 0.9411, 0.9555, 0.9669, 0.9759, 0.9825, 0.986461, 0.990254, 0.992622, 0.994254, 0.995035, 0.995771];
wearing_CHD_3DPCK = [0.786848, 0.856832, 0.900742, 0.931521, 0.951636, 0.964224, 0.973884, 0.980993, 0.985321, 0.990005, 0.993393, 0.995170, 0.996194, 0.996864, 0.997365, 0.997616];

% figure2: 3D PCK analysis of tracking in free motion
dorsum_3DPCK = [0.9543, 0.9688, 0.9789, 0.9847, 0.9885, 0.9918, 0.9945, 0.9965, 0.9975, 0.9981, 0.9986, 0.9988, 0.9992, 0.9994, 0.9994, 0.9995];
thumb_3DPCK = [0.9038, 0.9306, 0.9504, 0.9687, 0.9802, 0.9878, 0.9914, 0.9940, 0.9960, 0.9973, 0.9981, 0.9984, 0.9987, 0.9989, 0.9991, 0.9992];
index_3DPCK = [0.9293, 0.9590, 0.9778, 0.9860, 0.9898, 0.9912, 0.9923, 0.9937, 0.9952, 0.9967, 0.9980, 0.9988, 0.9993, 0.9995, 0.9997, 0.9999];
middle_3DPCK = [0.9211, 0.9480, 0.9653, 0.9785, 0.9869, 0.9921, 0.9952, 0.9967, 0.9979, 0.9987, 0.9992, 0.9994, 0.9996, 0.9997, 0.9998, 0.9998];
TotalMean_3DPCK = [0.7812, 0.8497, 0.8979, 0.9338, 0.9549, 0.9694, 0.9779, 0.9840, 0.9888, 0.9924, 0.9950, 0.9963, 0.9974, 0.9979, 0.9983, 0.9986];

% figure3: Histogram of frames, and mean errors versus the number of observed markers
TotalMeanError = [11.7202, 12.1932, 11.6957, 10.9485, 10.7943, 10.7010, 10.9078, 10.7828, 9.9113, 9.7664, 9.4398, 9.2446, 8.9008, 8.8885, 8.4375, 7.8677, 7.3115];
edges = [-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5];
relative_frequency = [0.00191818, 0.00258083, 0.00181355, 0.00252851, 0.00549297, 0.0139155, 0.0273079, 0.0475883, 0.0713215, 0.100373, 0.124211, 0.142974, 0.160465, 0.130855, 0.100234, 0.0518432, 0.0125205, 0.00163917, 0.00034876, 0.000052314, 0.000017438];

%% Plot Parameter Settings
plotParam.defaultColor = get(gca,'colororder');
plotParam.legendStr = {'Free Motion','Object Interaction','Wearing CHD'};
plotParam.legendStr2 = {'Dorsum','Thumb','Index','Middle','3D PCK'};
plotParam.legendStrNoMean = {'Dorsum','Thumb','Index','Middle'};

plotParam.FontSize1 = 19;
plotParam.FontSize2 = 13; % legend
plotParam.FontSize3 = 12; % axis

plotParam.LineStyle = {'-','--',':','-.','-'};
plotParam.MarkerEdgeColor = {'b','c','y','g','g','y','k'};
plotParam.MarkerShape = {'d','o','^','*','s'};
plotParam.MarkerSize = 8;
plotParam.LineWidth = 1.5;

%%
figure(1);

plot(error_thresholds, free_motion_3DPCK,'Marker', plotParam.MarkerShape{1},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(1,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{1});
hold on;
plot(error_thresholds, object_interaction_3DPCK,'Marker', plotParam.MarkerShape{2},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(2,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{2});
plot(error_thresholds, wearing_CHD_3DPCK,'Marker', plotParam.MarkerShape{3},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(3,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{3});

set(gca,'FontSize', plotParam.FontSize3);
grid on;
ylabel('Fraction of Frames','FontSize', plotParam.FontSize1);
xlabel('Error Thresholds (mm)','FontSize', plotParam.FontSize1);
ylim([0,1]);
legend(plotParam.legendStr, 'FontSize', plotParam.FontSize2,'Location','SouthEast');
title(strcat('3D PCK'),'FontSize',plotParam.FontSize1,'FontWeight','bold');

%%
figure(2);

plot(error_thresholds, dorsum_3DPCK,'Marker', plotParam.MarkerShape{1},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(1,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{1});
hold on;
plot(error_thresholds, thumb_3DPCK,'Marker', plotParam.MarkerShape{2},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(2,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{2});
plot(error_thresholds, index_3DPCK,'Marker', plotParam.MarkerShape{3},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(3,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{3});
plot(error_thresholds, middle_3DPCK,'Marker', plotParam.MarkerShape{4},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(4,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{4});
plot(error_thresholds, TotalMean_3DPCK,'Marker', plotParam.MarkerShape{5},'MarkerSize', plotParam.MarkerSize, 'MarkerFaceColor', plotParam.defaultColor(5,:),'LineWidth', plotParam.LineWidth, 'LineStyle', plotParam.LineStyle{5});

set(gca,'FontSize', plotParam.FontSize3);
grid on;
ylabel('Fraction of Frames','FontSize', plotParam.FontSize1);
xlabel('Error Thresholds (mm)','FontSize', plotParam.FontSize1);
ylim([0.7,1]);
legend(plotParam.legendStr2, 'FontSize', plotParam.FontSize2,'Location','SouthEast');
title(strcat('3D PCK'),'FontSize',plotParam.FontSize1,'FontWeight','bold');

%%
figure(3);

plot(TotalMeanError(), 'r', 'LineWidth',2);
ylim([0,30]); 
set(gca,'FontSize', plotParam.FontSize3)
ylabel('Total Mean Error (mm)','FontSize', plotParam.FontSize1)  
hold on;grid on;
yyaxis right
ax= gca;
ax.YColor = 'k';

h1 = histogram('BinEdges',edges, 'BinCounts',relative_frequency, 'FaceColor', '#4DBEEE','FaceAlpha',0.6);
title('Visual Marker Visibility','FontSize', plotParam.FontSize1,'FontWeight','bold')
ylabel('Histogram of Frames','FontSize', plotParam.FontSize1)
xlabel('Number of Observed Visual Markers','FontSize', plotParam.FontSize1)
legend('Total Mean Error','Histogram of Frames', 'FontSize', plotParam.FontSize2,'Location','NorthWest')
